package com.lnlyj.media.data.load;

import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.Arrays;
import java.util.Objects;

/**
 * Created by Wanglei on 2019/6/24.
 */

public class QueryConfig implements Parcelable {

    public static final int QUERY_TYPE_ALL = 1;
    public static final int QUERY_TYPE_ALBUM = 2;
    public static final int QUERY_TYPE_IMAGE = 3;
    public static final int QUERY_TYPE_VIDEO = 4;

    public Uri uri;
    public String[] projection;
    public String selection;
    public String[] selectionArgs;
    public String sortOrder = "date_modified DESC";
    public int queryType;


    protected QueryConfig(int queryType) {
        this.queryType = queryType;
    }

    protected QueryConfig(Parcel in) {
        uri = in.readParcelable(Uri.class.getClassLoader());
        projection = in.createStringArray();
        selection = in.readString();
        selectionArgs = in.createStringArray();
        sortOrder = in.readString();
        queryType = in.readInt();
    }

    public static final Creator<QueryConfig> CREATOR = new Creator<QueryConfig>() {
        @Override
        public QueryConfig createFromParcel(Parcel in) {
            return new QueryConfig(in);
        }

        @Override
        public QueryConfig[] newArray(int size) {
            return new QueryConfig[size];
        }
    };

    public boolean isQueryAlbum() {
        return queryType == QUERY_TYPE_ALBUM;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(uri, flags);
        dest.writeStringArray(projection);
        dest.writeString(selection);
        dest.writeStringArray(selectionArgs);
        dest.writeString(sortOrder);
        dest.writeInt(queryType);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        QueryConfig config = (QueryConfig) o;
        return queryType == config.queryType &&
                Objects.equals(uri, config.uri) &&
                Arrays.equals(projection, config.projection) &&
                Objects.equals(selection, config.selection) &&
                Arrays.equals(selectionArgs, config.selectionArgs) &&
                Objects.equals(sortOrder, config.sortOrder);
    }

    @Override
    public int hashCode() {
        int result = Objects.hash(uri, selection, sortOrder, queryType);
        result = 31 * result + Arrays.hashCode(projection);
        result = 31 * result + Arrays.hashCode(selectionArgs);
        return result;
    }
}
